import * as db from './db.js' //引入common
import store from './../store'

export function minToHour(min = 0) { return min / 60 }

export function bytesToHexString(inarray, size) {
	return new Promise((resolve, reject) => {
		let i, j, x;
		let hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
		let out = '';
		for (j = 0; j < size; ++j) {
			x = parseInt(inarray[j]) & 0xff;
			i = (x >> 4) & 0x0f;
			out += hex[i];
			i = x & 0x0f;
			out += hex[i];
		}
		resolve(out);;
	});
}

// 跳转
export function navigateTo(url, isLogin = false) {
	if (isLogin) {
		var userToken = db.get('token');
		if (!userToken) {
			navigateTo('/pagesCom/login/index')
		} else {
			uni.navigateTo({
				url: url,
				animationType: 'pop-in',
				animationDuration: 300
			})
		}
	} else {
		uni.navigateTo({
			url: url,
			animationType: 'pop-in',
			animationDuration: 300
		})
	}

}
// 关闭当前页面并跳转
export function redirectTo(url) { uni.redirectTo({ url: url, animationType: 'pop-in', animationDuration: 300 }) }
// 跳转tabbar
export function switchTab(url, switchName = '') {
	uni.setStorageSync('switchName', switchName)
	uni.switchTab({ url: url, animationType: 'pop-in', animationDuration: 300 })
}
// 复制
export function copyText(text) {
	uni.setClipboardData({
		data: text,
		success: function() {
			uni.showToast({
				title: '复制成功',
				icon: 'success',
				duration: 2000
			});
		},
		fail: function() {}
	});
}
// 判断是否登录
export function isLogin() { var userToken = db.get('token'); if (!userToken) { navigateTo('/pagesCom/login/index') } }

export function showErrorRes(req, title = '') { if (!req) { errorToShow(title); throw Error(title) } }

export function errorShow(title = '') { errorToShow(title); }

// 图片下载
export function downImg(imagePath = '') {
	uni.saveImageToPhotosAlbum({
		filePath: imagePath,
		success: function() {
			console.log('图片保存成功');
			// 成功处理逻辑
		},
		fail: function(err) {
			console.log('图片保存失败', err);
			// 失败处理逻辑
		}
	});
}

// 去掉金钱的无效0
export function formatNumber(number) {
	return (number * 100) / 100 + '';
}

export function graceNumber(number = 0) {
	if (number == 0) {
		return "0";
	} else if (number > 999 && number <= 9999) {
		return (number / 1000).toFixed(1) + 'k';
	} else if (number > 9999 && number <= 99999) {
		return (number / 10000).toFixed(1) + 'w';
	} else if (number > 99999) {
		return "10w+";
	}
	return number;

}

// 提示框
export function modelShow(
	title = '提示',
	content = '确认执行此操作吗?',
	callback = () => {},
	showCancel = true,
	cancelText = '取消',
	confirmText = '确定'
) {
	uni.showModal({
		title: title,
		content: content,
		showCancel: showCancel,
		cancelText: cancelText,
		confirmText: confirmText,
		cancelText: cancelText,
		confirmColor: '#FF802D',
		success: function(res) {
			if (res.confirm) {
				// 用户点击确定操作
				setTimeout(() => {
					callback(true)
				}, 500)
			} else if (res.cancel) {
				// 用户取消操作
				callback(false)
			}
		}
	})
}

function errorToShow(msg = '操作失败', callback = function() {}) {
	setTimeout(function() {
		uni.showToast({
			title: msg,
			icon: 'none',
			duration: 2000,
			success() {
				setTimeout(function() {
					callback()
				}, 2000)
			}
		})
	}, 100)
}

export function jumpToLogin() {

}